﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TestConsoleApp
{
    public class TestLockAndTask
    {
        private static object lock1 = new object();

        public static void Test()
        {
            Task.Run(() =>
            {
                Console.WriteLine("Init---Main Thread ID:" + Thread.CurrentThread.ManagedThreadId + " ---date:" + DateTime.Now.ToString("mm.ss.fff"));

                while (true)
                {

                    Init();



                    Thread.Sleep(10);
                }
            });

            Console.ReadLine();
        }

        public static async void Init()
        {
            string str = await Task.Run(() =>
            {
                Thread.Sleep(1000);
                Console.WriteLine("Task---str:qx ---Thread ID:" + Thread.CurrentThread.ManagedThreadId + " ---date:" + DateTime.Now.ToString("mm.ss.fff"), "Task");

                return "qx";
            });

            Init1(str);
        }


        public static void Init1(string str)
        {
            lock (lock1)
            {
                Thread.Sleep(1000);
                Console.WriteLine("Init---str:" + str + "---Thread ID:" + Thread.CurrentThread.ManagedThreadId + " ---date:" + DateTime.Now.ToString("mm.ss.fff"), "Init");
            }
        }
    }
}
